<?php
/**
 * Service_Mail
 *
 * Класс User_Notification осуществляет работу по оповещению пользователей о различных событиях
 *
 * @author Егоров Александр <slamer@slamer.ru>
 * @author Дмитрий Кузьма <kouzma.d@gmail.com>
 * @version 1.0
 */
class User_Notification {
  public static $arrNotifications = null;
	public function __construct() {}
	
	/**
	 * Получает список оповещений по электронной почте
	 * 
	 */
	private function getNotifications() {
		Service_Debug::setDebug();
		if (!self::$arrNotifications) {
			$objDB = Service_Database::instance();
			$stmt = "SELECT 
			  `notification`.`action`, `notification`.`subject`, `notification`.`text` 
			  FROM `notification`";
			$arrNotifications = $objDB->select($stmt);
			self::$arrNotifications = Service_ArrayConverter::makeKeyArrayFromMultyArray($arrNotifications, 'action');
	  }
		return self::$arrNotifications;
		
	}

  private function _mailHello() {
  	$arrNotifications = $this->getNotifications();
  	$hello = $arrNotifications['hello']['text'];
    $text = '<html>		
<table width="600" cellspacing="0" align="left" style="border-collapse: separate;" >
<!-- header -->
<tr>
	<td width="600" height="8" style="border-top:1px solid #135D9D; border-bottom:1px solid #171819; background:#045398;"></td>
</tr>
<tr>
	<td height="80" width="600" bgcolor="#f0f0f0" style="background:url(cid:bg.png) repeat-x #F0F0F0;border:1px solid #d3d5d7; border-top:0;">
		<table width="600" height="80" cellspacing="0">
			<tr>
				<td width="30"></td>
				<td style="vertical-align:middle;" width="500"><a href="http://hotels.ru"><img border="0" alt="Hotels.ru" src="cid:logo.png" /></td>
				<td style="vertical-align:middle;" width="20"><a href="http://vk.com"><img border="0" alt="Vkontakte" src="cid:vk.png" /></a></td>
				<td style="vertical-align:middle;" width="20"><a href="http://fb.com"><img border="0" alt="Facebook" src="cid:fb.png" /></a></td>
				<td width="10"></td>
			</tr>
		</table>
	</td>
</tr>
<!-- content -->
<tr>
	<td width="600" style="border-left:1px solid #d3d5d7;border-right:1px solid #d3d5d7;">
		<div style="padding:10px 20px"><h1 style="margin:0px;">' . $hello . '</h1>';
    return $text;
  }

  private function _mailEnd() {
  	$arrNotifications = $this->getNotifications();
  	$text = $arrNotifications['end']['text'];
  	$text = str_replace('[site]', SITE_NAME, $text);
  	$text = str_replace('[url]', W_FULLPATH, $text);
    return $text . '</div>
		</td>
	</tr>
	<!-- footer -->
	<tr style="vertical-align:middle;">
		<td height="68" width="600" bgcolor="#f2f6e8" style="border:1px solid #d3d5d7;">
			<table width="600" cellspacing="0">
				<tr valign="bottom">
					<td width="30"></td>
					<td width="190"><b>Бесплатно по России</b></td>
					<td width="190"><b>Санкт-Петербург</b></td>
					<td width="190"><b>Москва</b></td>
				</tr>
				<tr valign="top">
					<td></td>
					<td><font size="4"><b>8 800 555-09-21</b></font></td>
					<td><font size="4"><b>8 812 309-57-60</b></font></td>
					<td><font size="4"><b>8 495 505-63-25</b></font></td>
				</tr>
			</table>
		</td>
	</tr>
</table></html>';
  }
  
  private function _send($strEMail, $subject, $text) {
  	$text = $this->_cssText($text);
    $m = new Service_Amail( 
    	$strEMail, 
			$subject,
			self::_mailHello() . $text . self::_mailEnd(),                    
			"From: " . EMAIL_FROM . "\n"
		);
		$m->file(IMAGES . 'mail/bg.png');
		$m->file(IMAGES . 'mail/vk.png');
		$m->file(IMAGES . 'mail/fb.png');
		$m->file(IMAGES . 'mail/logo.png');
    return $m->send();
  }
  
  private function _cssText($text) {
  	$text = str_replace('<p>', '<p style="color:#000000; font-family: Arial, Helvetica; margin:3px; padding:0px;">', $text);
  	$text = str_replace('<div class="header"><h2>', '<div style="padding:5px 10px;background:#e9f2fb;margin-top:20px;"><h2 style="margin:0px;font-size:20px;">', $text);
  	$text = str_replace('<div class="block"', '<div style="padding:0 10px;""', $text);
  	return $text;
  }

  /**
   * Метод уведомляет пользователя о его регистрации
   *
   * @param string $strEMail
   * @param string $login
   * @param string $pass
   */
  public function newAccount($strEMail, $login, $pass) {
    $arrNotifications = $this->getNotifications();
    $subject = "[" . SITE_NAME . "] " . $arrNotifications['new_account']['subject'];
    $text = $arrNotifications['new_account']['text'];
    $code = Service_Generate::generateApproveCode($strEMail, $login); 
    $confirmationLink = W_FULLPATH . 'auth/approve/' . $code . '/' . $strEMail;
    $text = str_replace('[site]', SITE_NAME, $text);
    $text = str_replace('[email]', $strEMail, $text);
  	$text = str_replace('[login]', $login, $text);
  	$text = str_replace('[pass]', $pass, $text);
  	$text = str_replace('[url]', $confirmationLink, $text);
    
  	return $this->_send($strEMail, $subject, $text);
  }

  /**
   * Метод уведомляет пользователя о создании для него нового аккаунта
   *
   * @param string $strEMail
   * @param string $login
   * @param string $pass
   */
  public function createAccount($strEMail, $login, $pass) {
    $arrNotifications = $this->getNotifications();
    $subject = "[" . SITE_NAME . "] " . $arrNotifications['create_account']['subject'];
    $text = $arrNotifications['create_account']['text'];
    
    $code = Service_Generate::generateApproveCode($strEMail, $login); 
    $confirmationLink = W_FULLPATH . 'auth/approve/' . $code . '/' . $strEMail;
    $text = str_replace('[site]', SITE_NAME, $text);
  	$text = str_replace('[email]', $strEMail, $text);
  	$text = str_replace('[login]', $login, $text);
  	$text = str_replace('[pass]', $pass, $text);
  	$text = str_replace('[url]', $confirmationLink, $text);
    
  	return $this->_send($strEMail, $subject, $text);
  }

  /**
   * Метод уведомляет пользователя о создании для него нового аккаунта
   *
   * @param string $strEMail
   * @param string $login
   * @param string $pass
   */
  public function createHotelAccount($strEMail, $login, $pass) {
    $arrNotifications = $this->getNotifications();
    $subject = "[" . SITE_NAME . "] " . $arrNotifications['create_hotel_account']['subject'];
    $text = $arrNotifications['create_hotel_account']['text'];
    
    $code = Service_Generate::generateApproveCode($strEMail, $login); 
    $text = str_replace('[site]', SITE_NAME, $text);
  	$text = str_replace('[email]', $strEMail, $text);
  	$text = str_replace('[login]', $login, $text);
  	$text = str_replace('[pass]', $pass, $text);
    
  	return $this->_send($strEMail, $subject, $text);
  }
  
  /**
   * Оповещает пользователя о создании ему нового пароля
   *
   * @param string $strEMail
   */
  public function recovery($strEMail, $pass) {
    $action = 'recovery';
  	$arrNotifications = $this->getNotifications();
    $subject = "[" . SITE_NAME . "] " . $arrNotifications[$action]['subject'];
    $text = $arrNotifications[$action]['text'];

    $text = str_replace('[site]', SITE_NAME, $text);
  	$text = str_replace('[pass]', $pass, $text);
    
  	return $this->_send($strEMail, $subject, $text);
  }
     
  /**
   * Оповещает администратора о новом фидбэке
   *
   * @param array $arrFeedback - массив с данными
   */
  public function feedback($arrFeedback) {
    $subject = "[" . SITE_NAME . "] Новый feedback";
    $text = '<br />';
    foreach ($arrFeedback as $key => $value) {
    	$text .= str_replace('_', ' ', Service_Function::utf8_first_to_upper($key));
    	$text .= ': ' . $value . '<br />';
    }
  	return $this->_send('spb@hotels.ru', $subject, $text);
  }
  

}
?>